//
//  MainSetPriceVC.m
//  ZITHER
//
//  Created by weixin on 2018/7/26.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainSetPriceVC.h"
#import <CoreGraphics/CoreGraphics.h>
#import "MainSearchBottom.h"

@interface MainSetPriceVC ()<QMUITextFieldDelegate>
@property (nonatomic,strong)QMUITextField *fieldMinPrice;
@property (nonatomic,strong)QMUITextField *fieldMinPrice2;
@property (nonatomic,strong)MainSearchBottom *bottomView;
@end

@implementation MainSetPriceVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setNav];
    [self setUI];
    
    
    //监听键盘，键盘出现
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(keyboardwill:)
                                                name:UIKeyboardWillShowNotification object:nil];
    
    //监听键盘
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(keybaordhide:)
                                                name:UIKeyboardWillHideNotification object:nil];
    
}



-(void)setNav{
    [self.baseNavigationView setHiddenNav:YES VC:self];
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.navViewStyle.title.text = @"";
    self.baseNavigationView.navViewStyle.leftBtn.hidden = YES;
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.bottomLineView.hidden = YES;
    
    BaseLab *lab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 1) text:@"搜索条件    >    价格范围（¥）" font:1 textColor:[UIColor colorWithHex:@"333333"] isBold:NO numberOfLines:0];
    lab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
    [self.view addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(TabbarHeight-5);
        make.left.equalTo(self.view).offset(15);
    }];
    

    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
//    [self.baseNavigationView setBackgroundColor:[UIColor clearColor]];
//    self.baseNavigationView.bottomLineView.backgroundColor = [UIColor clearColor];
//    self.baseNavigationView.leftButton1.hidden = YES;
//    [UIColor setDefaulGradiennBackgroundColors:self.view];
//
//    QMUILabel *leb = [[QMUILabel alloc]initWithFrame:CGRectMake(20,50, 200, 40)];
//    leb.text = @"选择价格";
//    leb.font = [UIFont systemFontOfSize:18];
//    leb.textColor = [UIColor whiteColor];
//    [self.view addSubview:leb];
}


-(void)setUI{

    self.fieldMinPrice = [[QMUITextField alloc]initWithFrame:CGRectMake(0, 100, 150, 50)];
    self.fieldMinPrice.font = [UIFont systemFontOfSize:25];
    self.fieldMinPrice.delegate = self;
    self.fieldMinPrice.placeholderColor = [UIColor whiteColor]; // 自定义 placeholder 的颜色
    self.fieldMinPrice.returnKeyType = UIReturnKeyDone;
    self.fieldMinPrice.enablesReturnKeyAutomatically = YES;
    self.fieldMinPrice.tintColor = [UIColor whiteColor];
//    self.fieldMinPrice.typingAttributes = @{NSFontAttributeName: UIFontMake(35),
//                              NSForegroundColorAttributeName: [UIColor whiteColor],
//                              NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:40]};
    
    // 限制可输入的字符长度
    self.fieldMinPrice.maximumTextLength = 8;
    self.fieldMinPrice.qmui_borderWidth = 2;
    self.fieldMinPrice.qmui_borderPosition = QMUIViewBorderPositionBottom;
    self.fieldMinPrice.keyboardType = UIKeyboardTypeNumberPad;
    self.fieldMinPrice.qmui_borderColor = [UIColor whiteColor];
//    self.fieldMinPrice.keyboardAppearance = UIKeyboardAppearanceDark;
    [self.fieldMinPrice setCornerRadiusWithRadius:8 isMasks:YES];
//    [self.fieldMinPrice setBorderWithColor:[UIColor whiteColor] width:YES];
    self.fieldMinPrice.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
    self.fieldMinPrice.backgroundColor = [UIColor colorWithHex:@"F5F5F5"];
    self.fieldMinPrice.textAlignment = NSTextAlignmentCenter;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"最低价" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"999999"],NSFontAttributeName:[UIFont fontWithName:@"PingFang-SC-Regular" size:20], NSParagraphStyleAttributeName:style}];
    self.fieldMinPrice.attributedPlaceholder = attri;
    [self.view addSubview:self.fieldMinPrice];
    
    
    UIView * lab = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 100)];
    [lab setCornerRadiusWithRadius:1 isMasks:YES];
    lab.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lab];
    
    self.fieldMinPrice2 = [[QMUITextField alloc]initWithFrame:CGRectMake(0, 100, 150, 50)];
    self.fieldMinPrice2.font = [UIFont systemFontOfSize:25];
    self.fieldMinPrice2.placeholderColor = [UIColor whiteColor]; // 自定义 placeholder 的颜色
    self.fieldMinPrice2.returnKeyType = UIReturnKeyDone;
    self.fieldMinPrice2.delegate = self;
    self.fieldMinPrice2.enablesReturnKeyAutomatically = YES;
    self.fieldMinPrice2.tintColor = [UIColor whiteColor];
    
//    self.fieldMinPrice2.typingAttributes = @{NSFontAttributeName: UIFontMake(35),
//                                       NSForegroundColorAttributeName: [UIColor whiteColor],
//                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:40]};
    
    
    // 限制可输入的字符长度
    self.fieldMinPrice2.maximumTextLength = 8;
    self.fieldMinPrice2.qmui_borderWidth = 2;
    self.fieldMinPrice2.qmui_borderPosition = QMUIViewBorderPositionBottom;
    self.fieldMinPrice2.keyboardType = UIKeyboardTypeNumberPad;
    self.fieldMinPrice2.qmui_borderColor = [UIColor whiteColor];
//    self.fieldMinPrice2.keyboardAppearance = UIKeyboardAppearanceDark;
    [self.fieldMinPrice2 setCornerRadiusWithRadius:8 isMasks:YES];
//    [self.fieldMinPrice2 setBorderWithColor:[UIColor whiteColor] width:YES];
    self.fieldMinPrice2.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
    self.fieldMinPrice2.backgroundColor = [UIColor colorWithHex:@"F5F5F5"];
//    self.fieldMinPrice2.textAlignment = NSTextAlignmentCenter;
    attri = [[NSAttributedString alloc] initWithString:@"最高价" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"999999"],NSFontAttributeName:[UIFont fontWithName:@"PingFang-SC-Regular" size:20], NSParagraphStyleAttributeName:style}];
    self.fieldMinPrice2.attributedPlaceholder = attri;

//    self.fieldMinPrice2.
    [self.view addSubview:self.fieldMinPrice2];

    
    self.fieldMinPrice.frame = CGRectMake(10, 150, 150, 60);
    self.fieldMinPrice2.frame = CGRectMake(kScreenW-160,150, 150, 60);
    lab.frame = CGRectMake(170, 150, kScreenW-170*2, 3);
    lab.centerY = self.fieldMinPrice.centerY;
    
    
    
    [self.view addSubview:self.bottomView];

}


#pragma mark - <QMUITextViewDelegate>

- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
 
}


- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [QMUITips showWithText:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] inView:self.view hideAfterDelay:2.0];
}



// 可以利用这个 delegate 来监听发送按钮的事件，当然，如果你习惯以前的方式的话，也可以继续在 textView:shouldChangeTextInRange:replacementText: 里处理
- (BOOL)textViewShouldReturn:(QMUITextView *)textView {
    if (self.fieldMinPrice.text.length !=0 &&self.fieldMinPrice2.text.length !=0) {
        //确定跳回去
        if (self.blcokPrice) {
            NSString *str = [NSString stringWithFormat:@"%@ - %@",self.fieldMinPrice.text,self.fieldMinPrice2.text];
            self.blcokPrice(str);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        self.blcokPrice(@"不限");
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
    if (self.fieldMinPrice.text.length !=0 &&self.fieldMinPrice2.text.length !=0) {
        //确定跳回去
        if (self.blcokPrice) {
            NSString *str = [NSString stringWithFormat:@"%@ - %@",self.fieldMinPrice.text,self.fieldMinPrice2.text];
            self.blcokPrice(str);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        self.blcokPrice(@"不限");
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    return YES;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    


    if ([self.fieldMinPrice isEqual:textField]) {
        if (self.fieldMinPrice2.text.length>0) {
            self.bottomView.btnDone.selected = YES;
        }else{
            self.bottomView.btnDone.selected = NO;
        }
    }else{
        if (self.fieldMinPrice.text.length>0) {
            self.bottomView.btnDone.selected = YES;
        }else{
            self.bottomView.btnDone.selected = NO;
        }
    }
    if (self.fieldMinPrice.text.length>0 && self.fieldMinPrice2.text.length>0 ) {
    }else{
    }
    
    return YES;
}




















//当键盘出现时，调用此方法
-(void)keyboardwill:(NSNotification *)sender
{
    //获取键盘高度
    NSDictionary *dict=[sender userInfo];
    NSValue *value=[dict objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardrect = [value CGRectValue];
    int height=keyboardrect.size.height;
    CGRect f = self.bottomView.frame;
    f.origin.y =   f.origin.y - height;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomView.frame = f;
    }];
    
}
//当键盘时候，视图回到原定
-(void)keybaordhide:(NSNotification *)sender
{
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomView.frame = CGRectMake(0, kScreenH-50, kScreenW, 50);
    }];
}

-(MainSearchBottom *)bottomView{
    if (!_bottomView) {
        _bottomView = [[MainSearchBottom alloc]initWithFrame:CGRectMake(0, kScreenH-50, kScreenW, 50)];
        _bottomView.backgroundColor = [UIColor colorWithHex:@"969696"];
        [_bottomView setState:Title];
  
        
        @weakify(self);
        [[_bottomView.btnCancel rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [[_bottomView.btnDone rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            NSString *str = @"";
            
            
            if (self.fieldMinPrice.text.length !=0 ||self.fieldMinPrice2.text.length !=0) {
                
                
                if ([self.fieldMinPrice.text intValue]>[self.fieldMinPrice2.text intValue]) {
                    NSString *price = self.fieldMinPrice2.text;
                    self.fieldMinPrice2.text = self.fieldMinPrice.text;
                    self.fieldMinPrice.text = price;
                }
                
                if (self.fieldMinPrice.text.length == 0) {
                    str = [NSString stringWithFormat:@"0 - %@",self.fieldMinPrice2.text];
                }else if (self.fieldMinPrice2.text.length == 0){
                    str = [NSString stringWithFormat:@"%@ - 10000000",self.fieldMinPrice.text];
                }else{
                    str = [NSString stringWithFormat:@"%@ - %@",self.fieldMinPrice.text,self.fieldMinPrice2.text];
                }
                
            }else{
                str = @"价格范围";
            }
            
            self.blcokPrice(str);
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
    }
    return _bottomView;
}
@end
